import axios from "axios";
import qs from "qs";

axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"; //POST配置请求头
// axios.defaults.baseURL = process.env.VUE_APP_BASE_URL; //配置接口域名地址

// axios请求头封装token
axios.interceptors.request.use(
  config => {
    let token = window.localStorage.getItem("token");
    // if (token) {
    //   config.headers.common["token"] = token; //将token设置成请求头
    // } else {
    //   config.headers.common["token"] = "";
    // }

    if (config.method === "post") {
      config.data = qs.stringify(config.data);
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);


//请求返回状态(响应拦截器)
axios.interceptors.response.use(
  res => {
    return Promise.resolve(res.data);
  },
  error => {
    return Promise.reject(error);
  }
);

// ---------------------封装GET  POST 请求
export function PostRequest(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
}

////返回一个Promise(发送get请求)
export function GetRequest(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, { params: params })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
}

// 并发请求
export function PostAll(url1, url2, params1, params2) {
  return new Promise((resolve, reject) => {
    // 创建promise，在promise中调用axios then里使用resolve回调，catch里使用reject回调
    var p1 = new Promise((resolve, reject) => {
      axios
        .post(url1, params1)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });

    var p2 = new Promise((resolve, reject) => {
      axios
        .post(url2, params2)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });

    //调用Promise.all().then(res=>{})
    Promise.all([p1, p2])
      .then(res => {
        resolve(res);
        // console.log(res);
      })
      .catch(err => {
        reject(err);
        // console.log(err);
      });
  });
}

// 并发请求
// export  function getProfile(){
//   //请求1
//   return axios.get('/profile');
// }
// function getUser(){
//   //请求2
//   return axios.get('/user');
// }
// //并发请求
// axios.all([
//   getProfile(),
//   getUser()
// ]).then(axios.spread((res1, res2)=>{
//   //两个请求现已完成
//  console.log(res1);
//   console.log(res2);
// }));

// 封装post请求 FormData方式
var instance = axios.create({
  headers: { "content-type": "multipart/form-data" } //formdata请求头
});
export function PostFormData(url, data = {}) {
  return new Promise((resolve, reject) => {
    instance
      .post(url, data)
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
}

// 封装post请求 FormData方式
var jsonPost = axios.create({
  headers: { "content-type": "application/json" } //formdata请求头
});
export function PostJson(url, data = {}) {
  return new Promise((resolve, reject) => {
    jsonPost
      .post(url, data)
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
}

export default {
  PostRequest,
  GetRequest,
  PostAll,
  PostFormData,
    PostJson
};
